#include "BondHisMarketStream.h"

#include "BondContainer.h"
#include "core/sync/lock.h"

/************************************************************************/
/* 日数据                                                                     */
/************************************************************************/
bool CBondHisMarketstreamDay::InsertBondInfoList(
    const std::list<xQBHisMarketstreamBondUnit_c>& bondStream) {
  std::string stCombondKey;
  for (std::list<xQBHisMarketstreamBondUnit_c>::const_iterator itr =
           bondStream.begin();
       itr != bondStream.end(); itr++) {
    stCombondKey = itr->m_bondkey;
    stCombondKey.append(".");
    stCombondKey.append(itr->m_listedmarket);
    //+"." + itr->m_listedmarket;
    CBondHisMarketstreamDealInfo& DealIf = m_mapDealInfo[stCombondKey];
    DealIf.m_BondInfo = *itr;
  }
  return true;
}

bool CBondHisMarketstreamDay::InsertBondDealList(
    std::list<xQBHisMarketstreamDealUnit_c> bondDeal) {
  std::string stCombondKey;
  for (std::list<xQBHisMarketstreamDealUnit_c>::const_iterator itr =
           bondDeal.begin();
       itr != bondDeal.end(); itr++) {
    stCombondKey = itr->m_bondkey;
    stCombondKey.append(".");
    stCombondKey.append(itr->m_listedmarket);

    CBondHisMarketstreamDealInfo& DealIf = m_mapDealInfo[stCombondKey];
    DealIf.m_Deal.push_back(*itr);
  }
  return true;
}

bool CBondHisMarketstreamDay::IsNCD(const std::string& stCombondKey) {
  static std::map<std::string, bool> m_stMapHisNcd;
  if (m_stMapHisNcd.find(stCombondKey) != m_stMapHisNcd.end()) {
    return m_stMapHisNcd[stCombondKey];
  }
  CBondInfo bondInfo;
  CBondContainer& bc = CBondContainer::instance();
  int bondIndex = bc.GetBondIndex(stCombondKey.c_str());
  if (bc.IsValidIndex(bondIndex)) {
    const CBondInfo& bondInfo = bc.ElementAtR(bondIndex);
    m_stMapHisNcd.insert(make_pair(stCombondKey, bondInfo.IsBondNCD()));
    return m_stMapHisNcd[stCombondKey];
  }
  m_stMapHisNcd.insert(make_pair(stCombondKey, false));
  return false;
}

CBondHisMarketstream::CBondHisMarketstream() {}

CBondHisMarketstream::~CBondHisMarketstream() {}

CBondHisMarketstream& CBondHisMarketstream::instance() {
  static CBondHisMarketstream s_bc;
  return s_bc;
}

bool CBondHisMarketstream::InsertBondInfoList(
    xQBHisMarketstreamBond_Ack_c* pData) {
  SCOPED_MUTEX(m_mutex);
  if (!pData) return false;
  CBondHisMarketstreamDay& hisDay = m_mapDayData[pData->m_HisDate];
  return hisDay.InsertBondInfoList(pData->m_List);
}

bool CBondHisMarketstream::InsertBondDealList(
    xQBHisMarketstreamDeal_Ack_c* pData) {
  SCOPED_MUTEX(m_mutex);
  if (!pData) return false;
  CBondHisMarketstreamDay& hisDay = m_mapDayData[pData->m_HisDate];
  return hisDay.InsertBondDealList(pData->m_List);
}

bool CBondHisMarketstream::GetMarketStreamHisDay(
    int date, CBondHisMarketstreamDay& dayData) {
  SCOPED_MUTEX(m_mutex);
  if (m_mapDayData.find(date) == m_mapDayData.end()) return false;
  dayData = m_mapDayData[date];
  return true;
}
